<template>
  <div class="list_all">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-table :data="adminsData" style="width: 100%">
      <el-table-column label="id" width="250" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row._id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="120" align="center">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>账户名: {{ scope.row.adminName }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.adminName }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="头像" width="80" align="center">
        <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)"
            type="primary"
            style="margin-left: 16px;"
          >修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row._id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      :append-to-body="true"
      :before-close="handleClose"
      :visible.sync="drawer"
      size="50%"
      align="center"
    >
      <div class="form-size" style="width:600px">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="140px"
          class="demo-ruleForm"
        >
          <h2 style="margin-bottom:30px">修改管理员信息</h2>
          <el-form-item label="新账户名:" prop="newadminsName">
            <el-input v-model.lazy="ruleForm.newadminsName" label-width="200px"></el-input>
          </el-form-item>
          <el-form-item label="新密码:" prop="newadminsPass">
            <el-input type="password" v-model.lazy="ruleForm.newadminsPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码:" prop="newcheckAdminsPass">
            <el-input type="password" v-model="ruleForm.newcheckAdminsPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="管理员权限:">
            <el-radio v-model="state_radio" label="common">启用</el-radio>
            <el-radio v-model="state_radio" label="down">禁用</el-radio>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions, mapMutations } = createNamespacedHelpers(
  "admins"
);
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.newcheckAdminsPass !== "") {
          this.$refs.ruleForm.validateField("newcheckAdminsPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newadminsPass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      drawer: false,
      rules: {
        newadminsPass: [{ validator: validatePass, trigger: "blur" }],
        newcheckAdminsPass: [{ validator: validatePass2, trigger: "blur" }],
        newadminsName: [{ validator: checkName, trigger: "blur" }],
      },
      state_radio: "common",
    };
  },
  computed: {
    ...mapState(["adminsData", "ruleForm"]),
  },
  created() {
    this.getAdmins();
  },
  methods: {
    ...mapActions(["getAdmins", "deleteAdmins", "updataAdmins"]),
    ...mapMutations(["setDrawer", "setInnerDrawer", "set_id", "reset"]),
    //打开编辑
    handleEdit(info) {
      this.drawer = !this.drawer;
      this.set_id(info);
    },
    //删除
    async handleDelete(_id) {
      const data = await this.deleteAdmins(_id);
      if (data.status) {
        this.getAdmins();
      } else {
        alert(data.message);
      }
    },
    //编辑确定
    handleClose(done) {
      this.$confirm("你确定要离开吗？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //新信息验证
    async submitForm(formName) {
      const data = await this.updataAdmins(this.state_radio);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (data.status) {
            alert("修改成功");
            this.getAdmins();
          }
        }
      });
    },
    resetForm(formName) {
      this.reset();
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./css/adminsList.scss";
</style>